<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                              |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\AuctionOrder;

use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\Address;
use AuctionCore\Other\Store;
use AuctionCore\Account\Back\Employee;

/**
 * 签约类
 * @author Angl
 */
class Signature
{
    public function __construct()
    {
    }

    /**
     * 获取签约日志
     * @param $order_id
     * @return mixed
     */
    public static function get_trace_log($order_id)
    {
        $cols = 'sl.createtime,sl.result,sl.remark,e.real_name as deliver,e.mobile as deliver_mobile';
        $list = M('signature_log')->alias('sl')->field($cols)
            ->join(C('DB_PREFIX') . "employee e ON e.emp_id=sl.emp_id")
            ->order("sl.createtime desc")
            ->where("sl.order_id=" . $order_id)->select();
        return $list;
    }


    /**
     * @param int $page
     * @param int $size
     * @param $params
     * @return array
     */
    public function get_list($page = 1, $size = 10, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;

        $map = [];
        $map['sl.result'] = $params['result'];
        //拍单状态
        if (!$params['auth']) {
            $map['co.deliver_id'] = array("eq", $params['emp_id']);
        }
        //拍单号
        if (!empty($params['order_no'])) {
            $map['o.order_no'] = $params['order_no'];
        }
        //所在地
        if (!empty($params['bc_city'])) {
            $map['c.location_area'] = array('in', $params['bc_city']);
        }
        //要查询的列表
        $cols = "o.order_id,o.order_no,o.is_lock,o.status,o.car_id,o.last_time,
            c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.location_area,co.seller_name,co.phone,co.sign_reserve_time,
            co.sign_reserve_province,co.sign_reserve_store,co.sign_service_method,co.sign_date,
            co.sign_reserve_city,co.sign_reserve_area,co.sign_reserve_address,co.deliver_id,o.is_sign_tracing";
        $prefix = C('DB_PREFIX');
        $total = M('signature_log')->alias('sl')
            ->join($prefix . "order as o ON o.order_id=sl.order_id")
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->count();

        $list = M('signature_log')->alias('sl')->field($cols)
            ->join($prefix . "order as o ON o.order_id=sl.order_id")
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->limit($size)->page($page)->order("sl.createtime DESC")->select();
        //echo M()->getLastSql();

        //数据处理
        if (!empty($list)) {
            $cm = new CarModel();
            $addr = new Address();
            $store = new Store();
            foreach ($list as $key => $val) {
                $list[$key] = $val;
                if ($list[$key]['is_sign_tracing'] == 0) {
                    $list[$key]['sign_reserve_time'] = transform_date($val['sign_reserve_time'], 1);
                }
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                if ($val['sign_service_method'] == 2) {
                    $province = $addr->get_name_by_code($val['sign_reserve_province']);
                    $city = $addr->get_name_by_code($val['sign_reserve_city']);
                    $area = $addr->get_name_by_code($val['sign_reserve_area']);
                    $list[$key]['address'] = $province . " " . $city . " " . $area . " " . $val['sign_reserve_address'];
                } elseif ($val['sign_service_method'] == 3) {
                    $address = $store->detail(array('store_id' => $val['sign_reserve_store']));
                    $list[$key]['address'] = $address['store_name'];
                } else {
                    $list[$key]['address'] = "未知";
                }
                $list[$key]['bc_city_name'] = $addr->get_name_by_code($val['location_area']);
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                if ($params['auth']) {
                    $deliver = Employee::user_info_id($val['deliver_id']);
                    $list[$key]['deliver_name'] = $deliver['real_name'];
                }
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }


    /**
     * 根据订单号得到签约信息
     *
     * @param int $orderId 订单号
     * @return mixed
     */
    public function getDetailByOrderId($orderId)
    {
        return M('signature_log')->where("order_id=" . $orderId)->find();
    }

    /**
     * 根据拍单ID获取签约信息
     * @param $orderId
     * @return bool|mixed
     */
    public static function getInfoByOrderId($orderId)
    {
        if($orderId<1) return false;
        $map = ["order_id"=>$orderId];
        return M("signature_log")->where($map)->find();
    }

    /**
     * 通过owner_id 获取账户信息
     * @param $owner_id
     * @return bool|mixed
     */
    public static function getAccountInfoByOwnerId($owner_id)
    {
        if($owner_id<1) return false;
        return M("signature_log")->where(['owner_id'=>$owner_id])->find();
    }
}

//end class
